<?php
namespace Biz\service\NiceGoodsTopic;

use Biz\model\NiceGoodsTopic\FirstTopic;
use Biz\model\NiceGoodsTopic\SecondTopic;
use Biz\model\NiceGoodsTopic\GoodsList as mGoodsList;
use Biz\cache\NiceGoodsTopic as cacheNiceGoodsTopic;
use Doraemon\tools\ImgUrl;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Biz\cache\wiki\WikiBasicCounter;
use Doraemon\service\statSign\Basic as basicStatSign;
use Biz\model\wiki\Wiki as mWiki;




class Topic
{

    public static function getDetailById($topic_id){
       $topicInfo =  SecondTopic::getInfo($topic_id);
       if(empty($topicInfo)) return [];
       $firInfo = FirstTopic::getInfo($topicInfo['pid']);
       if(empty($firInfo)) return [];
       $result = [];
       $result['id'] = $topic_id;
       $result['first_topic_name'] =$firInfo['name'];
       $result['topic_name']  = $topicInfo['topic_name'];
       $result['topic_desc'] =  $topicInfo['desc'];
       $result['cover_img'] =ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $topicInfo['img'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
       $result['join_num'] = cacheNiceGoodsTopic::getCount($topic_id) ? : 0;
        $avatar_list = [];
        if($result['join_num'] >0){
            $img_count = $result['join_num'] < 3 ?  $result['join_num'] : 3;
            $avatar_list = array_column(clsWiki::getRandUserCoverImg($img_count) , 'cover_img');
        }
       $result['avatar_list'] =  $avatar_list;
       $result['rela_topic_list'] =self::getRelaTopic($topicInfo['pid'] , $topic_id);
       return $result;
    }

    public static function getRelaTopic($pid , $topic_id)
    {
        $list =  SecondTopic::getRelaTopicByPid($pid , $topic_id ,['id'  , 'topic_name']);
        if($list && is_array($list)){
            foreach($list as &$val){
                $val['statSign'] = '';
            }
        }
        return $list;
    }

    public static function setCounters()
    {
        $topic_ids = SecondTopic::getByWhere( ['topic_status' => SecondTopic::STATUS_NORMAL] ,['id']);
        if($topic_ids && is_array($topic_ids)){
             foreach ($topic_ids as $id){
                 self::setCounter($id);
             }
        }
    }

    public static function  setCounter($topic_id)
    {
        $list = mGoodsList::getInfoByTopicId($topic_id);
        if($list && is_array($list)){
            $wikiIds = array_column($list , 'wiki_id');
            $wikiInfo = mWiki::selectByWhere([
                'status[!]' => mWiki::STATUS_DELETE,
                'id' => $wikiIds,
            ],['basic_id' ,'id']);
            if(empty($wikiInfo)) return cacheNiceGoodsTopic::setCount($topic_id , 0);
            $basicIds = array_column($wikiInfo ,'basic_id');
            $counters = WikiBasicCounter::getMulti($basicIds);
            $count = 0;
            foreach ($basicIds as $basicId) {
                $counter = (isset($counters[$basicId]) && $counters[$basicId]) ? $counters[$basicId] : [];
                if ($counter) {
                    $count += $counter[WikiBasicCounter::BASIC_FAVORITE] ? : 0;
                    $count += $counter[WikiBasicCounter::BASIC_CLICK_BUY_NUM] ? : 0;
                    $count += $counter[WikiBasicCounter::BASIC_DEFINED_NUM] ? : 0;
                }
            }
           cacheNiceGoodsTopic::setCount($topic_id , $count);
        }
        return $count;
    }


    public  static  function getJoinNumCounters($topic_ids)
    {
        $counters = cacheNiceGoodsTopic::getMultiCount($topic_ids);
        return $counters;

    }

    public static function getFirstTopicList()
    {
        $topicMap =   cacheNiceGoodsTopic::getFirsttopic();
        if(empty($topicMap)) return[];
        $secondList =  self::getSecondTopicByIds(array_values($topicMap));
        $firList = FirstTopic::getTopicList(['id' => array_keys($topicMap) , 'topic_status' => FirstTopic::STATUS_NORMAL]);
        $result = [];
        if($firList && is_array($firList)){
            $firList = array_combine(array_column($firList , 'id') , $firList);
            foreach ($topicMap as $key =>$val){
                if(empty($secondList[$val]))  continue;
                if(empty($firList[$key])) continue;
                $result[]=[
                    'id' => $firList[$key]['id'],
                    'first_topic_name' => "#".$firList[$key]['name'],
                    'second_topic_info' => $secondList[$val],

                ];
            }
        }
        return  $result;
    }

    public static function getSecondTopicByIds($topic_ids)
    {

        $secondList = SecondTopic::getByWhere([
            'topic_status' => FirstTopic::STATUS_NORMAL,
            'id' => $topic_ids,
            'show_time[<=]' => date("Y-m-d H:i:s"),
            'ORDER' => ['addtime' => 'DESC']
        ]);
        $topicJoinNum = self::getJoinNumCounters($topic_ids);
        if($secondList && is_array($secondList)){
            $secondList = array_combine(array_column($secondList , 'id')  , $secondList);
        }
        $return = [];
        foreach ($topic_ids as $secId){
            if(empty($secondList[$secId])) continue;
            $link = \Doraemon\tools\Hhzurl::H5NiceGoodsTopic($secondList[$secId]['id']);

            $avatar_list = [];
            if($topicJoinNum[$secId] >0){
                $img_count = $topicJoinNum[$secId] < 3 ?  $topicJoinNum[$secId] : 3;
                $avatar_list = array_column(clsWiki::getRandUserCoverImg($img_count) , 'cover_img');
            }
            $return[$secId] = [
                    'id' =>$secondList[$secId]['id'],
                    'topic_name' => $secondList[$secId]['topic_name'],
                    'cover_img' => ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $secondList[$secId]['img'], ImgUrl::IMAGE_SIZE_ORI_NPHONE),
                    'topic_desc' =>$secondList[$secId]['desc'],
                    'join_num' => $topicJoinNum[$secId],
                    'avatar_list'=>  $avatar_list,
                    'link' => \Biz\tools\helper\Common::formatFullScreenLink($link),
                    'statSign' =>basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_TOPIC_CARD, [
                        basicStatSign::EXTEND_KEY_OBJ_ID => $secondList[$secId]['id'],
                    ], ['topic_id' => $secondList[$secId]['id']])
            ];
        }
        return $return;
    }



    public static function setTopicCache()
    {
       $info =  SecondTopic::getHeadTopic();

       $result = [];
       if($info && is_array($info)){
           foreach($info as $val){
               if($result[$val['pid']]) continue;
               $result[$val['pid']] = $val['id'];
           }
           cacheNiceGoodsTopic::setFirsttopic(json_encode($result));
       }
    }






}